home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 May: Tool Chest / Dev.CD May 97 TC.toast / Sample Code / Processes / MP Threaded Sort / Sort / SortPictsThreads.cp < prev   
Encoding:
Text File  |  1997-03-13  |  3.9 KB  |  225 lines  |  [TEXT/CWIE]

  1. /*
  2.  
  3.     Macintosh Developer Technical Support MP Sample Code
  4.  
  5.     MPSortTask Modified by Matthew Xavier Mora
  6.     to show MP sorting and Blitting preemptively
  7.     
  8.     mxmora@apple.com
  9.     last modified 1/20/97
  10.     
  11.  
  12. */
  13.  
  14. #include "SortPicts.h"
  15. #include <stdio.h>
  16. #include <LowMem.h>
  17. //#include <DriverServices.h>
  18.  
  19. extern Boolean            gUseCoplandTasks;
  20. extern short            gTaskNumber;
  21.  
  22.  
  23. void    SortPicts::MakeThreaded( void)
  24. {
  25.     ThreadID                tempThreadInfo;
  26.     OSErr                    errWhatErr;
  27.     
  28.     unitCount = newUnitCount = 1;
  29.     
  30.     startTime = TickCount();
  31.  
  32.     if ( !coplandTask )
  33.     {
  34.         errWhatErr = NewThread( kCooperativeThread, 
  35.                                 (ThreadEntryProcPtr) SortPictsThreadEntry, 
  36.                                 (void *)this, 
  37.                                 20000,
  38.                                 kCreateIfNeeded,
  39.                                 (void**)nil,
  40.                                 &tempThreadInfo);
  41.             
  42.         threadInfo = tempThreadInfo;
  43.         
  44.         YieldToThread( threadInfo);
  45.     } else {
  46.     //    KernelProcessID        ourKPID;
  47.         OSStatus            errWhatTaskErr;
  48.     
  49.         // • Get our Kernel Process ID
  50.     //    ourKPID = CurrentKernelProcessID();
  51.     
  52.         // • Make a task ID...
  53.     //    sprintf( (char*)&myTaskName, "T%03.3d", gTaskNumber++ );
  54.         
  55.         // • Make sure the info on our pixels is current.
  56.         sortPixmap = GetGWorldPixMap( sortGWorld);
  57.         LockPixels( sortPixmap);
  58.         sortPixels = (SortPixelPtr) GetPixBaseAddr( sortPixmap);
  59.         HLock( (Handle) sortHandle);
  60.         sortData = *sortHandle;
  61.  
  62.         //UseSortData();
  63.         //UnuseSortData();
  64.  
  65.         errWhatTaskErr = MPCreateTask( (TaskProc)SortPictsThreadEntry, 
  66.                                     (void *)this, 
  67.                                     0,
  68.                                     NULL,
  69.                                      0,
  70.                                      0, 
  71.                                     0, 
  72.                                     &myTaskID);
  73.                                     
  74.          
  75.          (void)MPCreateCriticalRegion(&sortBlitterBusy);
  76.          
  77.         
  78.     }
  79. }
  80.  
  81.  
  82. pascal    voidPtr    SortPictsThreadEntry( void *there)
  83. {
  84.     SortPicts        *t = (SortPicts *)there;
  85.  
  86.     t->Entry();
  87.     
  88.     return nil;
  89. }
  90.  
  91.  
  92. void    SortPicts::Entry( void)
  93. {
  94.     while( 1)
  95.     {
  96.         Scramble();
  97.         Update();
  98.         Sort();
  99.         
  100.         if ( coplandTask ) {
  101.             //DelayFor( kDurationSecond ); 
  102.             Yield();
  103.             //MPYield();
  104.         }
  105.     }
  106. }
  107.  
  108.  
  109. /*
  110.  *    SetSortItem
  111.  *
  112.  */
  113. void    SortPicts::SetSortItem( long index, long data)
  114. {
  115. //static    long            horiz, vert;
  116.     long                offset;
  117. //static long                lastHorizLeft = 0, lastHorizRight = 0;
  118. //static long                pixelVert;
  119.  
  120. //    #ifdef USE_OPT_SETSORTITEM
  121. //    if( index < low)
  122. //        low = index;
  123. //    if( index > high)
  124. //        high = index;
  125. //    #else
  126.  
  127.     if ( sortData != *sortHandle )
  128.         DebugStr( "\pSetSortItem:  SortData is corupt." );
  129. #if 0
  130.     horiz = index % pictWidth;
  131.     vert = index / pictWidth;
  132.  
  133.     if( vert >= copyBitsRect.bottom)
  134.         copyBitsRect.bottom = vert+1;
  135.  
  136.     if( vert < copyBitsRect.top)
  137.         copyBitsRect.top = vert;
  138.     
  139.     offset = horiz + vert * pictRowBytes;
  140. #else
  141.     if( index > lastHorizLeft && index < lastHorizRight)
  142.     {
  143.     }
  144.     else
  145.     {
  146.         vert = index / pictWidth;
  147.         lastHorizLeft = vert * pictWidth;
  148.         lastHorizRight = lastHorizLeft + pictWidth;
  149.         
  150.         pixelVert = vert * pictRowBytes;
  151.  
  152.         if( vert >= copyBitsRect.bottom)
  153.             copyBitsRect.bottom = (short) vert+1;
  154.     
  155.         if( vert < copyBitsRect.top)
  156.             copyBitsRect.top = (short) vert;
  157.     }
  158.     horiz = index - lastHorizLeft;
  159.     
  160.     offset = horiz + pixelVert;
  161. #endif
  162.     sortPixels[offset] = (SortPixel) data;
  163.     //pixelBufferData[offset
  164.  
  165.     sortData[index] = data;
  166.     
  167.     if(!unitCount) {
  168.         Yield();
  169.     }
  170.     unitCount--;
  171.     
  172. }
  173.  
  174. void    SortPicts::ExchangeSortItem( long one, long theOther)
  175. {
  176.     long        temp;
  177.     
  178.     temp = sortData[one];
  179.     SetSortItem( one, sortData[theOther]);
  180.     SetSortItem( theOther, temp);
  181.     if (coplandTask) {  // Call this to simulate Idle
  182.         Idle(nil);
  183.     }
  184. }
  185.  
  186.  
  187. void    SortPicts::Yield( void)
  188. {
  189.     
  190.     if ( ! coplandTask )
  191.     {
  192.         CalculateNewUnitCount();
  193.         Update();
  194.         UnuseSortData();
  195.         YieldToAnyThread();
  196.         UseSortData();
  197.         startTime = TickCount();
  198.     } else {
  199.         MPYield();
  200.     }
  201.     
  202. }
  203.  
  204. void    SortPicts::CalculateNewUnitCount( void)
  205. {
  206.     long            deltaTime ;
  207.     
  208.     
  209.     if (coplandTask) {
  210.         deltaTime = LMGetTicks() - startTime;
  211.     
  212.     } else {
  213.         deltaTime = LMGetTicks() - startTime;
  214.     }
  215.     
  216.     if( deltaTime)
  217.         newUnitCount = (int) (((double) newUnitCount) * 
  218.                     ((double) kYieldTime / (double) deltaTime));
  219.     else
  220.         newUnitCount = newUnitCount * 6 + 1;
  221.     
  222.     unitCount = newUnitCount;
  223. }
  224.  
  225.